$(function(){
	var url={
		list:'{:url("adminapi/Carviolation/violationList")}'
	}
	$("#loading-first").fadeOut(500);
	var carload=layer.load(0, {
	  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
	laydate.render({
	  elem: '#violation_time',
	  type:"datetime"
	});
	{if $Think.get.id}
	var exitload=layer.load(0, {
	  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
	var dataexit={};
	var where_json={};
	where_json.violation_id=['in','{$Think.get.id}']
	dataexit.where_json=JSON.stringify(where_json)
	$.ajax({
		type: 'post',
		data: dataexit,
		url: url.list,
		success: function(res) {
			console.log(res)
			layer.close(exitload)
			for(var key in res.list[0]){
				$(`input[name="${key}"]`).val(res.list[0][key])
				$(`select[name="${key}"]`).data('id',res.list[0][key])
				$(`textarea[name="${key}"]`).val(res.list[0][key])
				if(key=='pic_list'){
					$('.pic_listfileimg').attr('src',res.list[0][key][0].pic_path)
				}
			}
			//获取车辆
			$.ajax({
				type:'post',
				data:{
					page:1,
					pagnum:10,
					where_json:JSON.stringify({company_id:$('.company_all_id').val()})
				},
				url:'{:url("/adminapi/Car/carList")}',
				success:function(res){
					layer.close(carload)
					if(res.code==1){
						if(res.data.length>0){
						console.log(res)
						var carlist=`<option value=''>请选择车辆</option>`;
						for(var i=0;i<res.data.length;i++){
							if($('.car').data('id')==res.data[i].car_id){
								carlist+=`<option selected value='${res.data[i].car_id}'>${res.data[i].number}</option>`
							}else{
								carlist+=`<option value='${res.data[i].car_id}'>${res.data[i].number}</option>`
							}
						}
						$('.car').html(carlist)
						 $('.car').selected();
						 }else{
							Err('该单位无车辆，请添加车辆')
						}
					}else{
						Err('该单位无车辆，请添加车辆')
					}
				},
				error:function(){
					layer.close(carload)
					Err('请求失败')
				}
			})
			//获取司机
			var driverload=layer.load(0, {
			  shade: [0.1,'#fff'] //0.1透明度的白色背景
			});
			
			var where_jsondriver={
				company_id:['in',$('.company_all_id').val()]
			}
			datadriver={
					page:1,
					pagenum:999999,
					fields:'driver_id,name'
				}
			datadriver.where_json=JSON.stringify(where_jsondriver)
			$.ajax({
				type:'post',
				url:'{:url("adminapi/Driver/driverList")}',
				data:datadriver,
				success:function(res){
					console.log(res)
					layer.close(driverload)
					if(res.code==1){
						if(res.data.length>0){
						console.log(res)
						var driverlist=`<option value=''>请选择司机</option>`;
						for(var i=0;i<res.data.length;i++){
							if($('.driver').data('id')==res.data[i].driver_id){
								driverlist+=`<option selected value='${res.data[i].driver_id}'>${res.data[i].name}</option>`
							}else{
								driverlist+=`<option value='${res.data[i].driver_id}'>${res.data[i].name}</option>`
							}
						}
						$('.driver').html(driverlist)
						 $('.driver').selected();
						 }else{
						 	Err('该单位无司机，请添加司机')
						 }
					}else{
						Err('该单位无司机，请添加司机')
					}
				},
				error:function(){
					layer.close(driverload)
					Err('请求失败')
				}
			})
		}
	})
	{/if}
	
	//获取车辆
			$.ajax({
				type:'post',
				data:{
					page:1,
					pagnum:10,
					where_json:JSON.stringify({company_id:$('.company_all_id').val()})
				},
				url:'{:url("/adminapi/Car/carList")}',
				success:function(res){
					layer.close(carload)
					if(res.code==1){
						if(res.data.length>0){
						console.log(res)
						var carlist=`<option value=''>请选择车辆</option>`;
						for(var i=0;i<res.data.length;i++){
							if($('.car').data('id')==res.data[i].car_id){
								carlist+=`<option selected value='${res.data[i].car_id}'>${res.data[i].number}</option>`
							}else{
								carlist+=`<option value='${res.data[i].car_id}'>${res.data[i].number}</option>`
							}
						}
						$('.car').html(carlist)
						 $('.car').selected();
						 }else{
							Err('该单位无车辆，请添加车辆')
						}
					}else{
						Err('该单位无车辆，请添加车辆')
					}
				},
				error:function(){
					layer.close(carload)
					Err('请求失败')
				}
			})
			//获取司机
			var driverload=layer.load(0, {
			  shade: [0.1,'#fff'] //0.1透明度的白色背景
			});
			
			var where_jsondriver={
				company_id:['in',$('.company_all_id').val()]
			}
			datadriver={
					page:1,
					pagenum:999999,
					fields:'driver_id,name'
				}
			datadriver.where_json=JSON.stringify(where_jsondriver)
			$.ajax({
				type:'post',
				url:'{:url("adminapi/Driver/driverList")}',
				data:datadriver,
				success:function(res){
					console.log(res)
					layer.close(driverload)
					if(res.code==1){
						if(res.data.length>0){
						console.log(res)
						var driverlist=`<option value=''>请选择司机</option>`;
						for(var i=0;i<res.data.length;i++){
							if($('.driver').data('id')==res.data[i].driver_id){
								driverlist+=`<option selected value='${res.data[i].driver_id}'>${res.data[i].name}</option>`
							}else{
								driverlist+=`<option value='${res.data[i].driver_id}'>${res.data[i].name}</option>`
							}
						}
						$('.driver').html(driverlist)
						 $('.driver').selected();
						 }else{
						 	Err('该单位无司机，请添加司机')
						 }
					}else{
						Err('该单位无司机，请添加司机')
					}
				},
				error:function(){
					layer.close(driverload)
					Err('请求失败')
				}
			})
	//点击添加图片
	$('button').on('click',function(){
		 	if($(this).data('type')=='file'){
		 		$(this).parent().find('input[type=file]').click()
		 	}
		});
//			$('input[type="file"]').on('change',function(){
//			if($(this).data('type')=='img'){
//				var fil=$(this)[0].files[0];
//				var that=this;
//				var reader = new FileReader();
//				reader.onload = function(evt) {
//					$(that).closest('.file-w').find('.fileimg').attr('src',evt.target.result)
//				}
//				reader.readAsDataURL(fil);
//			}
//		});
	$('.back').on('click',function(){
		window.history.back()
	}); 
	{if $Think.get.id}
	$('.keep2').on('click',function(){
		var dataadd={}
		dataadd.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		$('select').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		$('textarea').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		dataadd.violation_id={$Think.get.id};
		if($('.pic_list').val()){
			var file = $('.pic_list')[0].files[0]; 
			var formData = new FormData();
			var name = $('.pic_list').val();
			formData.append("image",file);
			formData.append("name",name);
			$.ajax({
				type:"post",
				url:"{:url('adminapi/Upload/uploadImg')}",
				data:formData,
				processData : false, 
				contentType : false,
				success:function(res){
					console.log(res.image)
					var pic_list=[]
					pic_list.push(res.image)
					dataadd.pic_list=JSON.stringify(pic_list)
					console.log(dataadd)
					
					repairexit(dataadd)
				}
			});
		}else {
            repairexit(dataadd);
		}

		
	})
	function repairexit(data){
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Carviolation/edit')}",
			data:data,
			success:function(res){
				console.log(res)
				if(res.code==1){
					Msg('保存成功')
					window.history.go(0)
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	
	
	{else/}
	$('.keep').on('click',function(){
		var dataadd={}
		dataadd.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		$('select').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		$('textarea').each(function(){
			if($(this).attr('name')){
				dataadd[$(this).attr('name')]=$(this).val()
			}
		})
		if($('.pic_list').val()){
			var file = $('.pic_list')[0].files[0]; 
			var formData = new FormData();
			var name = $('.pic_list').val();
			formData.append("image",file);
			formData.append("name",name);
			$.ajax({
				type:"post",
				url:"{:url('adminapi/Upload/uploadImg')}",
				data:formData,
				processData : false, 
				contentType : false,
				success:function(res){
					console.log(res.image)
					var pic_list=[]
					pic_list.push(res.image)
					dataadd.pic_list=JSON.stringify(pic_list)
					console.log(dataadd)
					
					repairadd(dataadd)
				}
			});
		}else{
            repairadd(dataadd);
		}
		
	})
	
	function repairadd(data){
		Load()
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Carviolation/add')}",
			data:data,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
					Msg('添加成功')
					window.location.href='{:url("violation")}'
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	}
	{/if}
})